package com.coin900.coin.server.config;

import com.coin900.coin.base.security.AnonFilter;
import com.coin900.coin.base.security.AuthenticationFilter;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.Filter;
import java.util.LinkedHashMap;
import java.util.Map;

@Configuration // 等价于beans
public class ShiroConfig {

    private static final Log logger = LogFactory.getLog(ShiroConfig.class);


    //不需要的注册的过滤器，不要用spring管理，springBoot会默认注册,
    @Bean
    public ShiroFilterFactoryBean shiroFilter(DefaultWebSecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean();
        shiroFilter.setSecurityManager(securityManager);
        shiroFilter.setLoginUrl("/tologin?viewType=page");
        shiroFilter.setSuccessUrl("/index");
        shiroFilter.setUnauthorizedUrl("/error");
        Map<String, Filter> filters = new LinkedHashMap<>();
        filters.put("anon", new AnonFilter());
        filters.put("authentication", new AuthenticationFilter());
        shiroFilter.setFilters(filters);
        Map<String, String> filterChainDefinitionMap = new LinkedHashMap<>();
        filterChainDefinitionMap.put("/", "anon");
        filterChainDefinitionMap.put("/index", "anon");
        filterChainDefinitionMap.put("/index.json", "anon");
        filterChainDefinitionMap.put("/tologin", "anon");
        filterChainDefinitionMap.put("/login.json", "anon");
        filterChainDefinitionMap.put("/project-center", "anon");
        filterChainDefinitionMap.put("/project-center.json", "anon");
        filterChainDefinitionMap.put("/partner.json", "anon");
        filterChainDefinitionMap.put("/partner", "anon");
        filterChainDefinitionMap.put("/detail/*", "anon");
        filterChainDefinitionMap.put("/registration", "anon");
        filterChainDefinitionMap.put("/registration.json", "anon");
        filterChainDefinitionMap.put("/code", "anon");
        filterChainDefinitionMap.put("/code.json", "anon");
        filterChainDefinitionMap.put("/aboutus", "anon");
        filterChainDefinitionMap.put("/aboutus.json", "anon");
        filterChainDefinitionMap.put("/regist/check_mobile.json", "anon");

        filterChainDefinitionMap.put("/assets", "anon");
        filterChainDefinitionMap.put("/assets.json", "anon");

        filterChainDefinitionMap.put("/forget-pwd", "anon");
        filterChainDefinitionMap.put("/pwd-modify", "anon");
        filterChainDefinitionMap.put("/send-forget-password.json", "anon");
        filterChainDefinitionMap.put("/forget-password.json", "anon");

        filterChainDefinitionMap.put("/sbi/*.json", "anon");
        filterChainDefinitionMap.put("/api/web", "anon");
        filterChainDefinitionMap.put("/wallet-balance.json", "anon");
        filterChainDefinitionMap.put("/coin/activate_mail", "anon");


        filterChainDefinitionMap.put("/coin/**", "anon");
        filterChainDefinitionMap.put("/coin/**.json", "anon");

        filterChainDefinitionMap.put("/**", "authentication");
        shiroFilter.setFilterChainDefinitionMap(filterChainDefinitionMap);
        return shiroFilter;
    }


}